﻿namespace flygame3
{
    // 棋盘格子类型枚举
    public enum GridType
    {
        Normal,      // 普通格
        StartPoint,  // 起飞点
        SafeZone,    // 安全区
        Jump,        // 跳跃格（前进指定步数）
        Trap,        // 陷阱格（暂停回合）
        FinalPath    // 终点冲刺区
    }

    // 棋盘格子类
    public class ChessGrid
    {
        public int Position { get; }      // 格子位置
        public GridType Type { get; }     // 格子类型
        public int? LinkedPosition { get; } // 跳跃关联位置

        public ChessGrid(int pos, GridType type, int? link = null)
        {
            Position = pos;
            Type = type;
            LinkedPosition = link;
        }

        // 处理格子事件
        public virtual void HandleEvent(Player player)
        {
            switch (Type)
            {
                case GridType.Jump when LinkedPosition.HasValue:
                    player.MoveTo(LinkedPosition.Value);
                    Console.WriteLine($"触发跳跃！跳转到位置{LinkedPosition}");
                    break;
                case GridType.Trap:
                    player.SkipTurn();
                    Console.WriteLine("触发陷阱！暂停一回合");
                    break;
            }
        }
    }

    // 玩家类
    public class Player
    {
        public int Id { get; }
        public int Position { get; private set; }
        public bool IsSkipTurn { get; private set; }

        public Player(int id) => Id = id;

        public void MoveTo(int newPosition) => Position = newPosition;
        public void SkipTurn() => IsSkipTurn = true;
        public void ResetTurn() => IsSkipTurn = false;
    }

    // 游戏棋盘类
    public class GameBoard
    {
        private int _currentPlayerIndex; // 当前玩家的索引
        private readonly Dictionary<int, ChessGrid> _grids = new(); // 棋盘格子字典
        private readonly List<Player> _players = new(); // 玩家列表

        /// <summary>
        /// 初始化棋盘
        /// </summary>
        public void Initialize()
        {
            // 构建棋盘格子（示例配置）
            _grids.Add(0, new ChessGrid(0, GridType.StartPoint));
            _grids.Add(1, new ChessGrid(1, GridType.Normal));
            _grids.Add(2, new ChessGrid(2, GridType.Jump, 5));
            // 更多格子配置...
            for (int i = 3; i <= 100; i++)
            {
                _grids.Add(i, new ChessGrid(i, GridType.Normal));
            }
        }

        public void AddPlayer(Player player) => _players.Add(player);

        public void MovePlayer(Player player, int steps)
        {
            int newPos = player.Position + steps;
            if (_grids.TryGetValue(newPos, out var grid))
            {
                player.MoveTo(newPos);
                grid.HandleEvent(player);
            }
        }

        // 渲染棋盘状态
        public void Render()
        {
            foreach (var grid in _grids.Values.OrderBy(g => g.Position))
            {
                Console.Write($"[{grid.Position}:{grid.Type.ToString()[0]}]");
            }
            Console.WriteLine();
        }


        // 获取当前活动玩家
        public Player GetCurrentPlayer()
        {
            // 循环检测可用玩家
            for (int i = 0; i < _players.Count; i++)
            {
                var player = _players[_currentPlayerIndex];

                if (!player.IsSkipTurn)
                    return player;

                // 自动解除跳过状态并切换下家
                player.ResetTurn();
                _currentPlayerIndex = (_currentPlayerIndex + 1) % _players.Count;
            }

            throw new InvalidOperationException("No active players");
        }

        // 切换到下一玩家
        public void MoveToNextPlayer()
        {
            _currentPlayerIndex = (_currentPlayerIndex + 1) % _players.Count;
        }
    }

    // 游戏主逻辑
    class Program
    {
        static void Main()
        {
            var board = new GameBoard();
            board.Initialize();
            board.AddPlayer(new Player(1));
            board.AddPlayer(new Player(2));

            while (true)
            {
                var current = board.GetCurrentPlayer();
                Console.WriteLine($"轮到玩家 {current.Id}");

                // 用空格键来控制游戏的进度
                Console.WriteLine("按空格键继续...");
                ConsoleKeyInfo keyInfo = Console.ReadKey(intercept: true); // intercept参数为true时，按键不会显示在控制台上
                if (keyInfo.KeyChar == ' ')
                {
                    // 掷骰子逻辑
                    int steps = new Random().Next(1, 7);
                    Console.WriteLine($"玩家{current.Id}投了：{steps}");
                    board.MovePlayer(current, steps);
                    Console.WriteLine($"玩家{current.Id}移动到：{current.Position}");

                    // 检测胜利条件
                    if (current.Position >= 100)
                    {
                        Console.WriteLine($"玩家 {current.Id} 获胜！");
                        break;
                    }
                    else
                    {
                        Console.WriteLine(current.Position);
                    }

                    // 下一个玩家
                    board.MoveToNextPlayer();
                }
            }
        }
    }
}
